/**
 * Author:   e20160504
 * Date:     2018/6/6 21:18
 * Description:
 */
package com.kevin.demo.framwork;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.google.common.collect.Maps;
import com.kevin.demo.enums.ResultEnum;
import com.kevin.demo.exception.MyException;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class Result<T> {

    private Integer code;
    private String msg;

    private T data;

    public Result(ResultEnum resultEnum, T data) {
        this.code = resultEnum.getCode();
        this.msg = resultEnum.getMsg();
        this.data = data;
    }

    public Result(Integer code, String msg,T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Result(List data,Long totalCount) {
        Map<String,Object> pager = Maps.newHashMap();
        pager.put("count",totalCount);
        pager.put("rows",data);
        this.code = ResultEnum.SUCCESS.getCode();
        this.msg = ResultEnum.SUCCESS.getMsg();
        this.data = (T) pager;
    }


    public Result(MyException e,T data){
        this.code = e.getCode();
        this.msg = e.getMessage();
        this.data = data;
    }



    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}
